package com.example.logintest1.serviceimpl.service;

import com.example.logintest1.Entry.AdminMember;
import com.example.logintest1.Entry.AdminMemberDTO;
import com.example.logintest1.Entry.LoginUser;
import com.example.logintest1.Entry.ResponseResult;
import com.example.logintest1.mapper.AdminMemberMapper;
import com.example.logintest1.serviceimpl.LoginServcie;
import com.example.logintest1.until.JwtUtil;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Objects;

@Service
public class LoginServiceImpl implements LoginServcie {

    @Resource
    private AuthenticationManager authenticationManager;
    //    @Autowired
//    private RedisCache redisCache;
    @Resource
    AdminMemberMapper adminMemberMapper;

    @Override
    public ResponseResult login(AdminMember adminMember) {
//      AuthenticationManager authenticate  进行用户认证

        Authentication authenticate= null;;
        try {
            UsernamePasswordAuthenticationToken authenticationToken =
                    new UsernamePasswordAuthenticationToken(adminMember.getUser(), adminMember.getPas());
             authenticate = authenticationManager.authenticate(authenticationToken);
            // 认证成功后的逻辑处理
            System.out.println(new ResponseResult(200, "登录成功"));
        } catch (AuthenticationException e) {
            // 捕获并处理认证失败的异常
            System.out.println(new ResponseResult(401, "认证失败: " + e.getMessage()));
        }

//        如果认证没通过，给出对应提示
        if (Objects.isNull(authenticate)) {
            throw new RuntimeException("用户名或密码错误");
        }
//        如果认证通过了，使用userid生成jwt jwt存入ResponseResult返回
        //使用userid生成token
        LoginUser loginUser = (LoginUser) authenticate.getPrincipal();
        String userId = loginUser.getId().toString();
        String jwt = JwtUtil.createJWT(userId);

//        把完整的用户信息存入redis userid作为key
        //authenticate存入redis
//        redisCache.setCacheObject("login:"+userId,loginUser);
        adminMemberMapper.settoken(jwt, loginUser.getId());
        //把token响应给前端
        HashMap<String, String> map = new HashMap<>();
        map.put("token", jwt);
        return new ResponseResult(200, "登陆成功", map);
    }

//    @Override
//    public ResponseResult logout() {
////        获取SecurityContextHolder中的用户id
//        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
//        Long userid = loginUser.getId();
////        删除redis中的值
////        redisCache.deleteObject("login:"+userid);
//        userMapper.deltoken(loginUser.getId());
//        return new ResponseResult(200, "退出成功");
//    }


}
